package com.example.rumen;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.w3c.dom.Text;

import java.util.Objects;

public class Select_seat extends AppCompatActivity {

    private Button btn_xz;

    private Button btn_xp;
    private TextView txt_xz;
    private CheckBox checkbox1;
    private CheckBox checkbox2;
    private CheckBox checkbox3;
    private CheckBox checkbox4;
    private CheckBox checkbox5;
    private CheckBox checkbox6;
    private CheckBox checkbox7;
    private CheckBox checkbox8;
    private CheckBox checkbox9;
    private CheckBox checkbox10;
    private CheckBox checkbox11;
    private CheckBox checkbox12;
    private CheckBox checkbox13;
    private CheckBox checkbox14;
    private CheckBox checkbox15;
    private CheckBox checkbox16;
    private CheckBox checkbox17;
    private CheckBox checkbox18;
    private CheckBox checkbox19;
    private CheckBox checkbox20;
    private CheckBox checkbox21;
    private CheckBox checkbox22;
    private CheckBox checkbox23;
    private CheckBox checkbox24;
    private CheckBox[] checkBox;
    private String likes;
    private int count_no;
    private String filme;

    @SuppressLint("MissingInflatedId")//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_select_seat);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initView();
        btn_xz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                filme = getIntent().getStringExtra("filme");
                if(filme==null|| Objects.equals("",filme)){
                    Toast.makeText(getApplicationContext(), "您当前还未选择影片，请选择！！", Toast.LENGTH_LONG).show();
                    txt_xz.setText("您当前还未选择影片，请选择！！");
                    return;
                }
                count_no = 0;
                for (int i = 0; i < checkBox.length; i++) {
                    if (checkBox[i].isChecked() == false) {
                        count_no++;
                    }
                }
                if (count_no == checkBox.length) {
                    //提示用户
                    Toast.makeText(getApplicationContext(), "您当前还未选择座位，请选择！！", Toast.LENGTH_LONG).show();
                    txt_xz.setText("您当前还未选择座位，请选择！！");
                } else {

                    likes = "";
                    for (int i = 0; i < checkBox.length; i++) {
                        if (checkBox[i].isChecked() == true) {
                            likes += checkBox[i].getText().toString() + "、";
                        }
                    }
                    likes = likes.substring(0, likes.length() - 1);



                    AlertDialog.Builder builder = new AlertDialog.Builder(Select_seat.this);
                    builder.setIcon(R.drawable.hana);
                    builder.setTitle("温馨提示");
                    builder.setMessage("您当前选择的影片是《"+filme+"》\n座位是：" + likes + ",请您再次确认!!!");

                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //提示用户
                            Toast.makeText(getApplicationContext(), "您当前选择的影片是《"+filme+"》\n座位是：" + likes, Toast.LENGTH_LONG).show();
                            txt_xz.setText("您当前选择的影片是《"+filme+"》\n座位是：" + likes);
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            for (int i = 0; i < checkBox.length; i++) {
                                checkBox[i].setChecked(false);
                            }
                            //提示用户
                            Toast.makeText(getApplicationContext(), "您当前还未选择，请选择！！", Toast.LENGTH_LONG).show();
                            txt_xz.setText("您当前还未选择，请选择！！");
                        }
                    });
                    builder.show();
                }
            }
        });


        btn_xp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //页面跳转
                Intent intent = new Intent(getApplicationContext(), RadioBtn.class);
                startActivity(intent);
                finish();
            }
        });
    }


    private void initView() {

        btn_xz = findViewById(R.id.button_318_xz);
        btn_xp = findViewById(R.id.button_318_xp);
        txt_xz = findViewById(R.id.textView_318_film);

        checkbox1 = findViewById(R.id.checkBox_318_DP_1);
        checkbox2 = findViewById(R.id.checkBox_318_DP_2);
        checkbox3 = findViewById(R.id.checkBox_318_DP_3);
        checkbox4 = findViewById(R.id.checkBox_318_DP_4);
        checkbox5 = findViewById(R.id.checkBox_318_DP_5);
        checkbox6 = findViewById(R.id.checkBox_318_DP_6);
        checkbox7 = findViewById(R.id.checkBox_318_DP_7);
        checkbox8 = findViewById(R.id.checkBox_318_DP_8);
        checkbox9 = findViewById(R.id.checkBox_318_DP_9);
        checkbox10 = findViewById(R.id.checkBox_318_DP_10);
        checkbox11 = findViewById(R.id.checkBox_318_DP_11);
        checkbox12 = findViewById(R.id.checkBox_318_DP_12);
        checkbox13 = findViewById(R.id.checkBox_318_DP_13);
        checkbox14 = findViewById(R.id.checkBox_318_DP_14);
        checkbox15 = findViewById(R.id.checkBox_318_DP_15);
        checkbox16 = findViewById(R.id.checkBox_318_DP_16);
        checkbox17 = findViewById(R.id.checkBox_318_DP_17);
        checkbox18 = findViewById(R.id.checkBox_318_DP_18);
        checkbox19 = findViewById(R.id.checkBox_318_DP_19);
        checkbox20 = findViewById(R.id.checkBox_318_DP_20);
        checkbox21 = findViewById(R.id.checkBox_318_DP_21);
        checkbox22 = findViewById(R.id.checkBox_318_DP_22);
        checkbox23 = findViewById(R.id.checkBox_318_DP_23);
        checkbox24 = findViewById(R.id.checkBox_318_DP_24);

        checkBox = new CheckBox[]{checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, checkbox6,
                checkbox7, checkbox8, checkbox9, checkbox10, checkbox11, checkbox12,
                checkbox13, checkbox14, checkbox15, checkbox16, checkbox17, checkbox18,
                checkbox19, checkbox20, checkbox21, checkbox22, checkbox23, checkbox24};


    }
}